package com.mes.check.dao;

import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.mes.check.dao.iface.ICheckTaskAssignDao;
import com.mes.check.domain.CheckTask;
import com.mes.check.domain.CheckTaskAssign;
import com.mes.common.dao.BasicDao;
import com.mes.task.domain.Demand;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

@Repository
public class CheckTaskAssignDao extends BasicDao<CheckTaskAssign>implements ICheckTaskAssignDao{
	private static final Log log = LogFactory.getLog(CheckTaskAssignDao.class);
	 
	/**
	 * 获得某个检验员下的所有检验任务派工
	 * @param checkTaskUid
	 * @param page
	 * @return
	 */
	@Override
	public List<CheckTaskAssign> getCheckTaskAssignsByCheckerUid(String checkerUid, Page page) {
		List list=null;
		try {
			DetachedCriteria crit = DetachedCriteria.forClass(CheckTaskAssign.class);
			crit.createAlias("checkTask", "ct"); //一定要加一个别名，否则会报错
			crit.createAlias("ct.operationTask", "ot"); //一定要加一个别名，否则会报错
			crit.add(Restrictions.eq("checkerUid", checkerUid));
			crit.add(Restrictions.lt("ct.checkTaskState", CheckTask.CHECKTASKSTATE_COMPLETED));
			crit.addOrder(Property.forName("ot.priority").desc());
			crit.addOrder(Property.forName("assnStart").asc());
			
			//如果需要进行分页处理，则调用如下语句
			if (page != null) {
				this.pagingQuery(page, crit);
			} else {
				list = this.query(crit, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public List<CheckTaskAssign> getCheckTaskAssignsByCheckTaskUid(String checkTaskUid, Page page) {
		List list=null;
		try {
			DetachedCriteria crit = DetachedCriteria.forClass(CheckTaskAssign.class);
			crit.add(Restrictions.eq("checkTaskUid", checkTaskUid));
			crit.addOrder(Property.forName("createTime").desc());
			
			//如果需要进行分页处理，则调用如下语句
			if (page != null) {
				this.pagingQuery(page, crit);
			} else {
				list = this.query(crit, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	@Override
	public List<CheckTaskAssign> getCheckTaskAssignsByDept(String deptUid, Page page) {
		List list=null;
		try {
			DetachedCriteria crit = DetachedCriteria.forClass(CheckTaskAssign.class);
			crit.add(Restrictions.eq("checkTask.checkTaskType", CheckTask.CHECKTASKTYPE_SUMMARYCHECK));
			crit.add(Restrictions.ge("checkTask.checkTaskState", CheckTask.CHECKTASKSTATE_READY));
			crit.add(Restrictions.lt("checkTask.checkTaskState", CheckTask.CHECKTASKSTATE_COMPLETED));
			crit.add(Restrictions.eq("checker.deptUid", deptUid));
			crit.addOrder(Property.forName("checker.employeeName").asc());
			
			//如果需要进行分页处理，则调用如下语句
			if (page != null) {
				this.pagingQuery(page, crit);
			} else {
				list = this.query(crit, null);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return list;
	}

	
	@Override
	public void getCheckTaskAssigns(Page<CheckTaskAssign> page, Map<String, Object> parameter, Criteria criteria)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
